package com.bnuz.vtl.controller;

import com.bnuz.vtl.model.Comment;
import com.bnuz.vtl.model.Discuss;
import com.bnuz.vtl.model.Tag;
import com.bnuz.vtl.service.CommentService;
import com.bnuz.vtl.service.DiscussService;
import com.bnuz.vtl.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/api/community")
@RestController
public class CommunityController {

    @Autowired
    private DiscussService discussService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private TagService tagService;


    // Discuss API
    @PostMapping("/discuss")
    public Discuss saveDiscuss(@RequestBody Discuss discuss) throws Exception{
        return discussService.save(discuss);
    }

    @PostMapping("/discuss/{privateNodeId}")
    public Discuss saveDiscuss(@RequestBody Discuss discuss,@PathVariable("privateNodeId") String privateNodeId) throws Exception{
        return discussService.save(discuss);
    }

    @DeleteMapping("/discuss/{discussId}")
    public void deleteByDiscussId(@PathVariable("discussId") String discussId) throws Exception{
        discussService.deleteById(discussId);
    }

    @GetMapping("/discuss")
    public List<Discuss> findDiscussAll() throws Exception{
        return discussService.findAll();
    }

    @GetMapping("/discuss/{discussId}")
    public Discuss findByDiscussId(@PathVariable("discussId") String discussId) throws Exception{
        return discussService.findById(discussId);
    }

    // Comment API
    @PostMapping("/comment")
    public Comment saveComment(@RequestBody Comment comment) throws Exception{
        return commentService.save(comment);
    }

    @DeleteMapping("/comment/{commentId}")
    public void deleteByCommentId(@PathVariable("commentId") String commentId) throws Exception{
        commentService.deleteById(commentId);
    }

    @GetMapping("/comment")
    public List<Comment> findCommentAll() throws Exception{
        return commentService.findAll();
    }

    @GetMapping("/comment/{commentId}")
    public Comment findByCommentId(@PathVariable("commentId") String commentId) throws Exception {
        return commentService.findById(commentId);
    }


    // Tag API
    @PostMapping("/tag")
    public Tag saveTag(@RequestBody Tag tag) throws Exception{
        return tagService.save(tag);
    }

    @DeleteMapping("/tag/{tagId}")
    public void deleteById(@PathVariable("tagId") String tagId) throws Exception{
        tagService.deleteById(tagId);
    }

    @GetMapping("/tag")
    public List<Tag> findTagAll() throws Exception{
        return tagService.findAll();
    }

    @GetMapping("/tag/{tagId}")
    public Tag findTagById(@PathVariable("tagId") String tagId) throws Exception{
        return tagService.findById(tagId);
    }

}
